﻿using System;
using RazorEngine.Templating;

namespace TSharp.Core
{
    internal class TSharperDelegateActivator : IActivator
    {
        #region Fields

        private readonly Func<Type, object> Activator;

        #endregion

        #region Constructor

        /// <summary>
        /// Initialises a new instance of <see cref="TSharperDelegateActivator"/>.
        /// </summary>
        /// <param name="activator">The activator delegate.</param>
        public TSharperDelegateActivator(Func<Type, object> activator)
        {
            if (activator == null)
                throw new ArgumentNullException("activator");

            Activator = activator;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Creates an instance of the specified type.
        /// </summary>
        /// <param name="type">The type to create an instance.</param>
        /// <returns>The <see cref="ITemplate"/> instance.</returns>
        public ITemplate CreateInstance(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            return Activator(type) as ITemplate;
        }

        #endregion
    }
}